package com.simou.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simou.common.R;
import com.simou.entity.Category;
import com.simou.entity.Dish;
import com.simou.entity.Setmeal;
import com.simou.service.DishService;
import com.simou.service.Impl.CategoryServiceImpl;
import com.simou.service.Impl.DishServiceImpl;
import com.simou.service.Impl.SetmealServiceImpl;
import com.simou.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryServiceImpl categoryService;
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @GetMapping("/page")
    public R<Page> query(int page, int pageSize){
        log.info("page为{},pageSize为{}",page,pageSize);

        // 分页对象
        Page pageInfor = new Page(page, pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfor, queryWrapper);

        return R.success(pageInfor);
    }

    @PostMapping
    public R<Category> add(@RequestBody Category category){
        log.info("添加分类：{}",category);
        boolean save = categoryService.save(category);
        return save?R.success("添加分类成功"):R.error("添加分类失败");
    }

    @PutMapping
    public R<Category> modify(@RequestBody Category category){
        log.info("修改信息为；{}",category);
        boolean success = categoryService.updateById(category);
        return success?R.success("修改成功"):R.error("修改失败");
    }

    @DeleteMapping
    public R<Category> delete(Long ids){
        log.info("删除id为【{}】的分类");
        // 写法一
//        return categoryService.remove(ids);
        categoryService.remove(ids);
        return R.success("删除分类成功");
    }

//    获取category列表
    @GetMapping("/list")
    public R<Category> getCategoryList(String type){
        log.info("获取category列表");
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StringUtils.isNotEmpty(type), Category::getType,type);
        queryWrapper.orderByDesc(Category::getName);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }



}
